package com.asiainfo.iov.networkModel;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.asiainfo.iov.activity.BaseActivity;
import com.asiainfo.iov.configs.ConstantsME;
import com.asiainfo.iov.configs.InterfaceConfig;
import com.asiainfo.iov.entity.SaleListInfo;
import com.asiainfo.iov.entity.ScanInfo;
import com.asiainfo.iov.entity.StoreInfo;
import com.framework.utils.CollectionUtil;
import com.framework.utils.JSONParseUtil;
import com.framework.utils.PreferencesHelper;
import com.framework.utils.Y;
import com.framework2.okhttp3.HandlerCallback;
import com.framework2.okhttp3.Ok3Util;
import com.framework2.okhttp3.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * HttpUtil
 *
 * @author YobertJomi
 * className GohomePopupWindowUtil
 * created at  2017/3/17  13:01
 */
public class HttpUtil {
    private static volatile HttpUtil singleton;
    private DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance();

    private HttpUtil() {
    }

    public static HttpUtil getInstance() {
        if (singleton == null) {
            synchronized (HttpUtil.class) {
                if (singleton == null) {
                    singleton = new HttpUtil();
                }
            }
        }
        return singleton;
    }

    /**
     * 手机登录
     */
    public void requestLogin(String _interface, @NonNull String phone, @NonNull String pwd, final
    OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("password", pwd);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        Y.y("JSONRequestonResponse:" + result);
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String token = ParseResponseUtil.getInstance().parseReturnString(result, "userKey");
                            String nickName = ParseResponseUtil.getInstance().parseReturnString(result, "nickName");
                            String headPicUrl = ParseResponseUtil.getInstance().parseReturnString(result, "headPicUrl");
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                if (!TextUtils.isEmpty(token)) {
                                    resultListener.onSuccess(token, nickName, headPicUrl);
                                } else {
                                    resultListener.onFail(2, "未获取有效token");
                                }
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                        Y.y("JSONRequestonFailure:" + e.getMessage());
                    }
                });
        Ok3Util.getInstance().setBuilder(new OkHttpClient.Builder().connectTimeout(8000, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(false)).addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 手机登出/退出
     */
    public void requestLogout(String _interface, final OnRequestResult<String> resultListener) {

        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(null)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().setBuilder(new OkHttpClient.Builder().connectTimeout(8000, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(false)).addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 获取验证码（各种）
     */
    public void requestMobileVerify(String _interface, @NonNull int codeType, @NonNull String phone, final
    OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("codeType", codeType + "");
        map.put("mobile", phone);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        Y.y("JSONRequestonResponse:" + result);
                        String code = ParseResponseUtil.getInstance().parseVerifyCode(result);
                        String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                        if (null != resultListener && !TextUtils.isEmpty(code)) {
                            resultListener.onSuccess(code);
                        } else if (null != resultListener) {
                            resultListener.onFail(1, TextUtils.isEmpty(errorMsg) ? "获取验证码失败" : errorMsg);
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, "获取验证码失败");
                        }
                        Y.y("JSONRequestonFailure:" + e.getMessage());
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 手机号注册
     */
    public void requestRegister(String _interface, @NonNull String phone, @NonNull String pwd, @NonNull String
            captchaKey, @NonNull String captcha, final OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("userName", phone);
        map.put("mobile", phone);
        map.put("password", pwd);
        map.put("mobileValidVoucher", captchaKey);
        map.put("mobileVerifyCode", captcha);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        Y.y("JSONRequestonResponse:" + result);
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                        Y.y("JSONRequestonFailure:" + e.getMessage());
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 修改密码
     */
    public void requestModifyPwd(String _interface, @NonNull String pwd_pre, @NonNull String pwd_new, @NonNull String
            pwd_new_again, final OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("oldPassword", pwd_pre);
        map.put("newPassword", pwd_new);
        map.put("reNewPassword", pwd_new_again);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 忘记密码/重置密码
     */
    public void requestForgetPwd(String _interface, @NonNull String phone, @NonNull String pwd, @NonNull String
            captchaKey, @NonNull String captcha, final OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("newPassword", pwd);
        map.put("reNewPassword", pwd);
        map.put("smsValidVoucher", captchaKey);
        map.put("codeContent", captcha);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 修改昵称
     */
    public void requestModifyNick(String _interface, @NonNull String nick, final OnRequestResult<String>
            resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("nickName", nick);
        StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 三方登录检查是否绑定手机号
     */
    public void requestThirdLoginCheckBinding(String _interface, int type, @NonNull String openId, final String nick,
                                              final String imgUrl, final OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("loginResouce", type + "");
        map.put("openId", openId);
        map.put("nickname", nick);
        map.put("headPicUrl", imgUrl);
        StringRequest jsonRequest = StringRequest.getBuilder(true).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            String mobile = ParseResponseUtil.getInstance().parseReturnString(result, "mobile");
                            String userKey = ParseResponseUtil.getInstance().parseReturnString(result, "userKey");
                            String nickName = ParseResponseUtil.getInstance().parseReturnString(result, "nickName");
                            String headPicUrl = ParseResponseUtil.getInstance().parseReturnString(result, "headPicUrl");
                            if (success) {
                                resultListener.onSuccess(mobile, userKey, nickName, headPicUrl);
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 三方登录请求绑定手机号
     */
    public void requestThirdLoginBindPhone(String _interface, int type, final String openId, @NonNull String phone,
                                           final String mobileValidVoucher, final String moblieVerifyCode, final
                                           String password, final
                                           OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("loginResouce", type + "");
        map.put("mobile", phone);
        map.put("mobileValidVoucher", mobileValidVoucher);
        map.put("moblieVerifyCode", moblieVerifyCode);
        map.put("password", password);
        map.put("openId", openId);
        StringRequest jsonRequest = StringRequest.getBuilder(true).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            String mobile = ParseResponseUtil.getInstance().parseReturnString(result, "mobile");
                            String token = ParseResponseUtil.getInstance().parseReturnString(result, "userKey");
                            String nickName = ParseResponseUtil.getInstance().parseReturnString(result, "nickName");
                            String headPicUrl = ParseResponseUtil.getInstance().parseReturnString(result, "headPicUrl");
                            if (success && !TextUtils.isEmpty(mobile)) {
                                resultListener.onSuccess("true", token, nickName, headPicUrl);
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 绑定社交账号
     */
    public void requestBindQQorWchat(final BaseActivity baseActivity, String _interface, final
    OnRequestResult<String> resultListener) {
        StringRequest jsonRequest = StringRequest.getBuilder(true).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .get()
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        Y.y("JSONRequestonResponse_requestHomepageSaleDetail:" + result);
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            JSONArray array = ParseResponseUtil.getInstance().parseReturnJSONArray(result);
                            if (success) {
                                if (null != array && array.length() > 0) {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject jsonObject = array.optJSONObject(i);
                                        if (jsonObject != null) {
                                            if (1 == jsonObject.optInt("loginSource")) {
                                                PreferencesHelper.getInstance().putInfo(ConstantsME.qqBind,
                                                        !TextUtils.isEmpty(jsonObject.optString("openId")));
                                                if (array.length() == 1)
                                                    PreferencesHelper.getInstance().putInfo(ConstantsME.wChatBind,
                                                            false);
                                            }
                                            if (2 == jsonObject.optInt("loginSource")) {
                                                PreferencesHelper.getInstance().putInfo(ConstantsME.wChatBind,
                                                        !TextUtils.isEmpty(jsonObject.optString("openId")));
                                                if (array.length() == 1)
                                                    PreferencesHelper.getInstance().putInfo(ConstantsME.qqBind, false);
                                            }
                                        }
                                    }
                                }
                                resultListener.onSuccess("true");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 根据shopId查询我的店铺详情
     */
    public void requestShopDetail(final BaseActivity baseActivity, String _interface, String shopId, final
    OnRequestResult<String> resultListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("shopId", shopId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        StringRequest jsonRequest = StringRequest.getBuilder(true).url(InterfaceConfig.BASE_SERVER_URL + _interface)
                .get(jsonObject)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            String provinceName = ParseResponseUtil.getInstance().parseReturnString(result,
                                    "provinceName");
                            String cityName = ParseResponseUtil.getInstance().parseReturnString(result, "cityName");
                            String location = ParseResponseUtil.getInstance().parseReturnString(result, "location");
                            if (success) {
                                resultListener.onSuccess(String.valueOf(provinceName + cityName + location));
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 出库--查询IMEI
     */
    public void requestQueryQRcode(final BaseActivity baseActivity, String _interface, @NonNull final String shopId,
                                   @NonNull final String imei, final OnRequestResult<ScanInfo> resultListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("shopId", shopId);
            jsonObject.put("imeiNo", imei);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL +
                _interface).get(jsonObject)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            String fullName = ParseResponseUtil.getInstance().parseReturnString(result, "fullName");
                            String brandName = ParseResponseUtil.getInstance().parseReturnString(result, "brandName");
                            String phoneType = ParseResponseUtil.getInstance().parseReturnString(result, "categoryId");
                            String suggestPrice = ParseResponseUtil.getInstance().parseReturnString(result,
                                    "suggestPrice");
                            String imgUrl = ParseResponseUtil.getInstance().parseReturnString(result,
                                    "smallMobileImgUrl");
                            String color = ParseResponseUtil.getInstance().parseReturnString(result, "color");
                            String room = ParseResponseUtil.getInstance().parseReturnString(result, "storageSize");
                            int remainQuantity = ParseResponseUtil.getInstance().parseReturnInt(result,
                                    "remainQuantity");
                            decimalFormat.applyPattern("0.00");
                            double _suggestPrice = 0;
                            try {
                                _suggestPrice = Double.parseDouble(suggestPrice);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (success && remainQuantity > 0) {
                                ScanInfo info = new ScanInfo();
                                info.setName(fullName);
                                info.setImei(imei);
                                info.setPrice(_suggestPrice);
                                info.setType(phoneType);
                                info.setImgUrl(imgUrl);
                                info.setContent(brandName);
                                info.setRoom(room);
                                info.setColor(color);
                                info.setIsPhone(true);
                                resultListener.onSuccess(info);
                            } else {
                                resultListener.onFail(1, "库存可能不存在该IMEI号");
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().setBuilder(new OkHttpClient.Builder().connectTimeout(8000, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(false)).addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 出库--生成销售单
     */
    public void requestBuildSaleList(final BaseActivity baseActivity, String _interface, @NonNull final String
            shopId, @NonNull final String name, @NonNull final int contactType, @NonNull final String contact,
                                     @NonNull final
                                     String remark, List<SaleListInfo> saleInfoList, final OnRequestResult<String>
                                             resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("customerName", name);
        map.put("contactWay", contactType + "");
        map.put("customerLinktel", contact);
        map.put("remark", remark);
        JSONArray stockOutDetails = new JSONArray();
        try {
            int len = saleInfoList.size();
            for (int i = 0; i < len; i++) {
                SaleListInfo saleListInfo = saleInfoList.get(i);
                if (null != saleListInfo) {
                    boolean isPhone = TextUtils.equals("0", saleListInfo.getType());
                    ArrayList<ScanInfo> list = saleListInfo.getList();
                    if (!CollectionUtil.getInstance().isEmpty(list)) {
                        int listLen = list.size();
                        for (int j = 0; j < listLen; j++) {
                            ScanInfo scanInfo = list.get(j);
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("categoryId", TextUtils.isEmpty(scanInfo.getType()) ? "" : scanInfo
                                    .getType());
                            jsonObject.put("fullName", TextUtils.isEmpty(scanInfo.getName()) ? "" : scanInfo.getName());
                            jsonObject.put("imei", isPhone ? scanInfo.getImei() : "");//手机/配件
                            jsonObject.put("outboundPrice", scanInfo.getPrice());
                            jsonObject.put("productType", isPhone ? "1" : "2");
                            jsonObject.put("quantity", scanInfo.getCount());
                            jsonObject.put("remark", "");
                            stockOutDetails.put(jsonObject);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("stockOutDetails", stockOutDetails.toString());
        final StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL +
                _interface).post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            String profit = ParseResponseUtil.getInstance().parseReturnString(result, "profit");
                            String orderCode = ParseResponseUtil.getInstance().parseReturnString(result, "orderNo");
                            String intro = ParseResponseUtil.getInstance().parseReturnString(result, "intro");
                            String date = ParseResponseUtil.getInstance().parseReturnString(result, "date");
                            if (success) {
                                resultListener.onSuccess(profit, orderCode, intro, date);
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 库存--库存列表
     */
    public void requestStoreList(final BaseActivity baseActivity, String _interface, @NonNull final String shopId,
                                 @NonNull final String fullName, @NonNull final int page, @NonNull final int
                                         pageSize, final
                                 OnRequestListResult<StoreInfo> resultListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("shopId", shopId);
            jsonObject.put("fullName", fullName);
            jsonObject.put("pageNo", page);
            jsonObject.put("pageSize", pageSize);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL +
                _interface).get(jsonObject)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                ArrayList<StoreInfo> list = new ArrayList<>();
                                JSONObject jsonObject = ParseResponseUtil.getInstance().parseReturnJSONObject(result);
                                if (null != jsonObject) {
                                    String totalRecords = JSONParseUtil.getInstance().optString(jsonObject,
                                            "totalRecords");
                                    JSONArray array = JSONParseUtil.getInstance().optJSONArray(jsonObject,
                                            "queryInventoryDetail");
                                    if (null != array && array.length() > 0) {
                                        int len = array.length();
                                        for (int i = 0; i < len; i++) {
                                            JSONObject jsonOb = array.optJSONObject(i);
                                            if (null != jsonOb) {
                                                StoreInfo info = new StoreInfo();
                                                info.setName(JSONParseUtil.getInstance().optString(jsonOb,
                                                        "productName"));
                                                info.setCount(JSONParseUtil.getInstance().optInt(jsonOb, "quantity"));
                                                try {
                                                    info.setPrice(Double.parseDouble(JSONParseUtil.getInstance()
                                                            .optString(jsonOb, "suggestUnitPrice")));
                                                } catch (NumberFormatException e) {
                                                    info.setPrice(0);
                                                }
                                                info.setProductType(JSONParseUtil.getInstance().optInt(jsonOb,
                                                        "productType"));
                                                list.add(info);
                                            }
                                        }
                                    }
                                    resultListener.onSuccess(list, totalRecords);
                                } else {
                                    resultListener.onFail(0, errorMsg);
                                }
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 消息----删除一条消息
     */
    public void requestDeleteOneMessage(final BaseActivity baseActivity, String _interface, String msgId, final
    OnRequestResult<String> resultListener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("msgId", msgId);
        final StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL +
                _interface).post(map)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("删除消息成功");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * 消息----标记一条消息已读
     */
    public void requestRemarkOneMessage(final BaseActivity baseActivity, String _interface, String msgId, final
    OnRequestResult<String> resultListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("msgId", msgId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final StringRequest jsonRequest = StringRequest.getBuilder(false).url(InterfaceConfig.BASE_SERVER_URL +
                _interface).get(jsonObject)
                .build(new HandlerCallback() {
                    @Override
                    public void onResponseMainThread(Call call, String result) throws IOException {
                        if (null != resultListener) {
                            boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                            String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                            if (success) {
                                resultListener.onSuccess("标记已读成功");
                            } else {
                                resultListener.onFail(1, errorMsg);
                            }
                        }
                    }

                    @Override
                    public void onFailureMainThread(Call call, IOException e) {
                        if (null != resultListener) {
                            resultListener.onFail(0, e.getMessage());
                        }
                    }
                });
        Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
    }

    /**
     * app--版本更新检测
     */
    public void requestVersionUpdate2(final BaseActivity baseActivity, String checkUpdateUrl, final
    OnRequestResult<String> resultListener) {
        try {
            final StringRequest jsonRequest = StringRequest.getBuilder(false).url(checkUpdateUrl).get()
                    .build(new HandlerCallback() {
                        @Override
                        public void onResponseMainThread(Call call, String result) throws IOException {
                            if (null != resultListener) {
                                if (!TextUtils.isEmpty(result)) {
                                    Y.y("更新：" + result);
//                                    result= URLEncoder.encode(result,"gb2312");

//                                    buffer.append(new String(bytes,0,n,"UTF-8"));
                                    Y.y("更新：" + result.getBytes("UTF-8"));
                                    Y.y("更新：" + result.getBytes("GBK"));
                                    Y.y("更新：" + result.getBytes("ISO8859-1"));
                                    boolean success = ParseResponseUtil.getInstance().isSuccess(baseActivity, result);
                                    String errorMsg = ParseResponseUtil.getInstance().parseReturnStringError(result);
                                    int versionCode = ParseResponseUtil.getInstance().parseReturnInt(result,
                                            "versionCode");
                                    String versionName = ParseResponseUtil.getInstance().parseReturnString(result,
                                            "versionName");
                                    String downLoadUrl = ParseResponseUtil.getInstance().parseReturnString(result,
                                            "downLoadUrl");
                                    JSONArray updateMsgs = JSONParseUtil.getInstance().optJSONArray(JSONParseUtil
                                            .getInstance().optJSONObject(result, "data"), "updateMsgs");
                                    StringBuilder stringBuilder = new StringBuilder();
                                    if (updateMsgs != null && updateMsgs.length() > 0) {
                                        for (int i = 0; i < updateMsgs.length(); i++) {
                                            String item = updateMsgs.optString(i);
//                                            item="测试";
                                            stringBuilder.append((i + 1)).append(".").append(item.trim()).append("\n");
                                        }
                                    }
                                    if (success) {
                                        resultListener.onSuccess(versionCode + "", versionName, stringBuilder
                                                .toString(), downLoadUrl);
                                    } else {
                                        resultListener.onFail(1, errorMsg);
                                    }
                                } else {
                                    resultListener.onFail(1, "");
                                }
                            }
                        }

                        @Override
                        public void onFailureMainThread(Call call, IOException e) {
                            if (null != resultListener) {
                                resultListener.onFail(0, e.getMessage());
                            }
                        }
                    });
            Ok3Util.getInstance().addToRequestQueueAsynchoronous(jsonRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 一般的返回
     */
    public interface OnRequestResult<T> {
        void onSuccess(T... t);

        void onFail(int code, String msg);
    }

    /**
     * 返回列表
     *
     * @param <T>
     */
    public interface OnRequestListResult<T> {
        void onSuccess(List<T> list, String... msg);

        void onFail(int code, String msg);
    }
}
